/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package application;

import java.awt.*;
import java.io.IOException;
import javafx.event.Event;
import java.net.URL;
import java.util.Map;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

public class DiveController implements Initializable {

	/**
	 * Initializes the controller class.
	 */
	@FXML
	private TextField diveTitle;

	@FXML
	private Label depthError;

	@FXML
	private Label depthLabel;

	@FXML
	private Button doneBtn;

	@FXML
	private TextField timeBox;

	@FXML
	private ComboBox<Integer> depthBox;

	@FXML
	private Label plan1;

	@FXML
	private Label plan12;

	@FXML
	private Label plan2;

	@FXML
	private Label plan21;

	@FXML
	private Label plan11;

	@FXML
	private Rectangle rectangle;

	@FXML
	private Button repDive;

	@FXML
	private Label timeError;

	@FXML
	private Label profileError;

	@FXML
	private Label timeLabel;

	@FXML
	private Label plan;

	private ObservableList<Integer> dList = FXCollections.observableArrayList(
			40, 50, 60, 70, 80, 90, 100, 110, 120, 130);
	private int userDepth, userTime;

	/**
	 *
	 * @param e
	 */
	@FXML
	private void repDive(Event e) throws IOException {
		validateTime(e);
		validateName(e);
		setDepth(e);

		System.out.println(userDepth + " " + userTime + " "
				+ Main.currUserProfile);

		if (userDepth > 0 && userTime > 0 && Main.currUserProfile.length() > 0) {
			Main.currTable = Main.planner.addFirstDive(userDepth, userTime);

			Main.gotoScene("Repetitive.fxml", repDive, this);
		}

	}

	@FXML
	private void buttonAction(Event e) {
		validateTime(e);
		validateName(e);
		setDepth(e);

		System.out.println(userDepth + " " + userTime + " "
				+ Main.currUserProfile);

		if (userDepth > 0 && userTime > 0 && Main.currUserProfile.length() > 0) {
			Main.currTable = Main.planner.addFirstDive(userDepth, userTime);
			Main.userProfiles.put(Main.currUserProfile,
					Main.planner.getPlanner());

			Main.planner.endPlan();

			Main.gotoScene("Profile.fxml", doneBtn, this);
		}

	}

	@FXML
	private void validateName(Event event) {
		String newProfile = diveTitle.getText().trim();

		if (!Main.userProfiles.keySet().contains(newProfile)
				&& newProfile.length() > 0) {
			Main.currUserProfile = newProfile;

			profileError.setVisible(false);
		} else {
			Main.currUserProfile = "";

			profileError.setVisible(true);
		}
	}

	@FXML
	void setDepth(Event event) {
		if (depthBox.getValue() == null) {
			userDepth = 0;
			depthError.setVisible(true);
		} else {
			userDepth = depthBox.getValue();
			depthError.setVisible(false);
		}
		validateTime(event);
		System.out.println("SUCCESS: Changed user depth input to " + userDepth);
	}

	@FXML
	void validateTime(Event event) {
		String time = timeBox.getText().trim();

		if (time.length() == 0) {
			timeError.setText("Please enter a time.");
			timeError.setVisible(true);

			return;
		}

		if (userDepth == 0) {
			userTime = 0;

			return;
		}

		int newTime = 0;

		try {
			newTime = Integer.parseInt(time);

			if (newTime > 0) {
				userTime = newTime;

				int max = getMaxTime();
				if (newTime <= max) {
					userTime = roundTime(newTime);

					timeError.setText("");
					timeError.setVisible(false);
				} else {
					userTime = 0;

					timeError.setText("Time must be <= " + max);
					timeError.setVisible(true);
				}
			} else {
				userTime = 0;

				timeError.setText("Time must be greater than 0.");
				timeError.setVisible(true);
			}
		} catch (NumberFormatException nfe) {
			System.out.println("ERROR: Caught invalid user input.");

			timeError.setText("Time must be a positive integer.");
			userTime = 0;
		}
	}

	private int roundTime(int num) {
		Map<Character, Integer[]> table = Main.planner.getFirstTable().get(
				userDepth);

		for (int i = 'A'; i <= 'L'; i++) {
			Integer[] pair = table.get((char) i);

			if (pair != null) {
				if (num <= pair[0]) {
					return pair[0];
				}
			}
		}

		return num;
	}

	private int getMaxTime() {
		Map<Character, Integer[]> table = Main.planner.getFirstTable().get(
				userDepth);

		for (int i = 'L'; i >= 'A'; i--) {
			Integer[] pair = table.get((char) i);
			if (pair != null) {
				if (pair != FirstNAUITable.UNVIABLE) {
					return pair[0];
				}
			}
		}

		return -1;
	}

	@Override
	public void initialize(URL url, ResourceBundle rb) {
		// DivePlanner planner = new DivePlanner();

		depthBox.setItems(dList);
		userDepth = 0;
		userTime = 0;
		Main.currUserProfile = "";
		Main.planner.createNewPlan();

		System.out.println("set initials");
	}

}
